package com.iot.ammeter.ui.components.auth

import android.content.res.Configuration
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.iot.ammeter.R
import com.iot.ammeter.ui.theme.AmmeterAppTheme

@Composable
fun Login(login: (email: String, password: String, onFinish: ()->Unit) -> Unit) {
    var email by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }
    var enabled by remember { mutableStateOf(true) }

    Column {
        EmailTextField(email, Modifier.padding(0.dp, 10.dp)) { email = it }
        PasswordTextFiled(password, Modifier.padding(0.dp, 10.dp)) { password = it }
        Button(
            onClick = {
                enabled = false
                login(email, password) {
                    enabled = true
                }
            },
            enabled = enabled,
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 30.dp),
        ) {
            Text(
                text = stringResource(id = R.string.login),
                color = MaterialTheme.colorScheme.onPrimary
            )
        }
    }
}

@Preview(name = "login", showBackground = true)
@Preview(name = "login night", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun LoginPreview() = AmmeterAppTheme {
    Login { _, _, _-> }
}
